/*************************************************************************
 * Copyright 2008 David Almilli
 *  
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 *  
 *     http://www.apache.org/licenses/LICENSE-2.0 
 *      
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 **************************************************************************/
package com.tivo.hme.bananas;

import java.text.FieldPosition;
import java.text.Format;
import java.text.ParsePosition;

public class TimeFormat extends Format {

	private static final long serialVersionUID = 5029708902362507719L;
	
	private boolean includeSeconds;

	public TimeFormat() {
		this(false);
	}

	public TimeFormat(boolean includeSeconds) {
		this.includeSeconds = includeSeconds;
	}

	@Override
	public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
		if (toAppendTo == null) {
			toAppendTo = new StringBuffer();
		}
		Integer value = ((Integer)obj);
		if (value != null) {
			int hours = value/3600;
			int min = (value/60) % 60;
			toAppendTo.append(hours).append(':');
			if (min < 10) {
				toAppendTo.append('0');
			}
			toAppendTo.append(min);
			
			if (includeSeconds) {
				int seconds = value % 60;
				if (seconds < 10) {
					toAppendTo.append('0');
				}
				toAppendTo.append(':').append(seconds);
			}
		}
		return toAppendTo;
	}

	@Override
	public Object parseObject(String source, ParsePosition pos) {
		return null;
	}
}
